local courseId = ARGV[1]
local userId = ARGV[2]
-- string
local stockKey = "course:stock:" .. courseId
-- set
local grabbedKey = "course:isGrabbed"
-- 判断这个课是否还有名额
if (tonumber(redis.call('get', stockKey)) <= 0) then
    return 1
end

-- 判断用户是否抢过课
local member = userId
if (redis.call('sismember', grabbedKey, member) == 1) then
    return 2
end

-- 扣库存
redis.call('incrby', stockKey, '-1')
-- 保存用户信息到order
redis.call('sadd', grabbedKey, member)
return 0